<?php

class Sent_model extends MY_Model
{

	public function __construct()
	{
		parent::__construct();
	}
	
	public function head()
	{
		return $this->db
			->select("
				SQL_CALC_FOUND_ROWS s.*,
				(SELECT COUNT(id) FROM _sent_item WHERE sent_id = s.id AND send_attempt = '0') AS num_unsent,
				(SELECT COUNT(id) FROM _sent_item WHERE sent_id = s.id AND send_attempt = '1') AS num_sent,
				(SELECT COUNT(id) FROM _sent_item WHERE sent_id = s.id) AS total
				", FALSE)
			->from('sent AS s');
	}
	
	public function add($data = array())
	{
		$this->db->insert('sent', $data);
		
		return $this->db->insert_id();
	}

	public function get($options = array())
	{
		$this->head();

		// WHERE
		if(isset($options['id']))
			$this->db->where('s.id', $options['id']);
		
		if(isset($options['user_id']))
			$this->db->where('s.user_id', $options['user_id']);		
		
		$q = $this->db->get();

        if (!$q->num_rows())
            return false;

        return $q->row();
    }

	public function gets($options = array(), $limit = 0, $offset = 0)
	{
		$this
			->head()
			->order_by('s.id', 'desc');
			
		// WHERE
		if(isset($options['user_id']))
			$this->db->where('s.user_id', $options['user_id']);
		
		return parent::my_result($limit, $offset);
	}

	public function edit($data = array(), $options = array())
	{
		$this->db->update('sent', $data, $options);
	}

	public function report($id)
	{
		$q = $this->db->query("SELECT
            (SELECT COUNT(id) FROM {$this->db->dbprefix('sent_item')} WHERE sent_id = s.id) as total_item,
            (SELECT COUNT(id) FROM {$this->db->dbprefix('sent_item')} WHERE sent_id = s.id AND is_send = '1') as total_sent,
            (SELECT COUNT(id) FROM {$this->db->dbprefix('sent_item')} WHERE sent_id = s.id AND is_send = '0') as total_unsent,
            (SELECT COUNT(id) FROM {$this->db->dbprefix('sent_item')} WHERE sent_id = s.id AND status = '1') as total_read
            FROM {$this->db->dbprefix('sent')} AS s
            WHERE s.id = {$id}");

		return $q->row();

	}

	public function last_sent_report($user_id)
	{

		$q = $this->db->query("SELECT
            (SELECT COUNT(id) FROM {$this->db->dbprefix('sent_item')} WHERE sent_id = s.id) as total_item,
            (SELECT COUNT(id) FROM {$this->db->dbprefix('sent_item')} WHERE sent_id = s.id AND is_send = '1') as total_sent,
            (SELECT COUNT(id) FROM {$this->db->dbprefix('sent_item')} WHERE sent_id = s.id AND is_send = '0') as total_unsent,
            (SELECT COUNT(id) FROM {$this->db->dbprefix('sent_item')} WHERE sent_id = s.id AND status = '1') as total_read
            FROM {$this->db->dbprefix($this->_table)} AS s
            WHERE s.user_id = {$user_id}
            ORDER BY s.id DESC
            LIMIT 1");

		if (!$q->num_rows())
		{

			$data = array(
				'total_item' => 0,
				'total_sent' => 0,
				'total_unsent' => 0,
				'total_read' => 0
			);

			return (object)$data;
		}

		return $q->row();

	}

	public function delete($id)
	{
		$user_id = uid();

		$this->db->query("DELETE tbl1, tbl2, tbl3
            FROM {$this->db->dbprefix('sent')} AS tbl1
            LEFT JOIN {$this->db->dbprefix('sent_group')} AS tbl2 ON (tbl1.id = tbl2.sent_id)
            LEFT JOIN {$this->db->dbprefix('sent_item')} AS tbl3 ON (tbl1.id = tbl3.sent_id)
            WHERE tbl1.id = '{$id}' AND tbl1.user_id = '{$user_id}'");
	}
}

/* End of file sent_model.php */
